
package jimm;

//#sijapp cond.if modules_SMILES is "true" #
import java.io.*;

import java.util.Vector;
import javax.microedition.lcdui.*;

import java.io.*;

import jimm.util.ResourceBundle;
import DrawControls.*;
import jimm.GM;
import jimm.comm.Util;


import javax.microedition.io.*;
import javax.wireless.messaging.*;

import java.io.IOException;

import javax.wireless.messaging.*;

public class PhoneBook
    implements CommandListener, Runnable {

    /** user interface command for indicating Send request */
    Command sendCommand = new Command(ResourceBundle.getString("sms_txt"), Command.OK, 3);
    Command callCommand = new Command(ResourceBundle.getString("call_txt"), Command.OK, 4);
//    Command okCommand = new Command("Ok", Command.OK, 1);
    /** user interface command for going back to the previous screen */
    Command backCommand = new Command(ResourceBundle.getString("back"), Jimm.is_smart_SE() ? Command.CANCEL : Command.BACK, 5);
    /** Display to use. */
    Display display;
    /** The URL to send the message to */

    TextField telnum;

	Form telnumfrm;

    /** Area where the user enters a message to send */
    TextBox messageBox;
    /** Where to return if the user hits "Back" */
    Displayable backScreen;
	
    Form callsms;
    /** Displayed when a message is being sent */
//    Displayable sendingScreen;
    
    /**
     * Initialize the MIDlet with the current display object and
     * graphical components. 
     */
    public PhoneBook(Display display) 
    {
        this.display = display;
    }

    public void promptAndSend(Displayable backScreen)
    {
        this.backScreen = backScreen;

	telnum = new TextField(ResourceBundle.getString("enter_number"), null, 50, TextField.PHONENUMBER);

        messageBox = new TextBox(ResourceBundle.getString("enter_message"), null, 65535, TextField.ANY);
        messageBox.addCommand(backCommand);
        messageBox.addCommand(sendCommand);
        messageBox.setCommandListener(this);

	telnumfrm = new Form("");

        telnumfrm.addCommand(backCommand);
        telnumfrm.addCommand(callCommand);
	telnumfrm.addCommand(sendCommand);
        telnumfrm.setCommandListener(this);

	telnumfrm.append(telnum);


        display.setCurrent(telnumfrm);
    }

    private void clearAll()
	{
		telnum = null;
		messageBox = null;
		telnumfrm = null;
		System.gc();
	}    

    public void commandAction(Command c, Displayable s) 
	{
        	if (c == backCommand) 
		{
			if (s == messageBox)
				display.setCurrent(telnumfrm);
			else
				display.setCurrent(backScreen);
			clearAll();
		} 
		else 
		if (c == sendCommand) 
		{
			if (s == telnumfrm)
			{
				display.setCurrent(messageBox);
			}
			else
			{
	                	display.setCurrent(backScreen); //ContactList.activate(); //display.setCurrent(sendingScreen);
        	        	new Thread(this).start();
			}
		}
		else
		if (c == callCommand)
		{
			display.setCurrent(backScreen);
			try
			{
				Jimm.jimm.platformRequest("tel:" + telnum.getString());
				clearAll();
			}       				

			catch (Exception e) { }
		}
    }
    
    public void run() {
           
        MessageConnection smsconn = null;
        try {
            /** Open the message connection. */

      MessageConnection conn = (MessageConnection) Connector.open ("sms://"+telnum.getString());
      TextMessage msg =
         (TextMessage)conn.newMessage (MessageConnection.TEXT_MESSAGE);
       msg.setPayloadText (messageBox.getString());
       conn.send (msg);

        } catch (Throwable t) {
//		System.out.println("Send caught: ");
//		t.printStackTrace();
        }
        if (smsconn != null) {
            try {
                smsconn.close();

		clearAll();

		display.setCurrent(backScreen);
            } catch (IOException ioe) {
//		System.out.println("Closing connection caught: ");
//		ioe.printStackTrace();
            }                
        }
    }
}
